* robustness_1yr_formal_bite_indccustry_ccity.do	JEP	14/03/19
* Does regressions that are in the term paper, for formal workers: RIF4
* Monthly wages
* Exclude some industries
* Do regressions by city - industry
* Do for 1998 q1
* Cluster by city

*************************** Change log ********************************** 

/*
	

clear 
version 13.1

* Project info
cap project, doinfo
if _rc==198 {
	if c(os)=="Unix" loc master "/home/jperez/AA_Minimum_Wage" 
 else if c(username)=="J16339" loc master "B:\Col_Minimum_Wage"
	else loc master "C:/Users/jorpp/Dropbox (Brown)/Col_Minimum_Wage"
 loc pr = 0
}
else {
	local master "`r(pdir)'"
	local doname "`r(dofile)'"  
 loc pr = 1
	* Project calls
	project, uses("`master'/Data/Source/ocupados_mw.dta")
	* Call programs
	project, relies_on("`master'/Code/Est/sample_choice.do")
	project, relies_on("`master'/Code/Est/build_mw_measures.do")
	project, relies_on("`master'/Code/Est/build_treatment.do")
	project, relies_on("`master'/Code/Est/export_rif.do")
	project, relies_on("`master'/Code/Est/confirmedrun.do")
	project, relies_on("`master'/Code/Est/graph_rif.do")
}
*/

loc master "\\bmstginveco\Salario_Minimo\Col_Minimum_Wage"
loc pr = 0
* Call necessary programs. I do not call using project to carry the programs around. I do not put these in the ado/personal folder because they are specific to the project. They are not ado files.
do "`master'/Code/Est/sample_choice.do"
do "`master'/Code/Est/build_mw_measures.do"
do "`master'/Code/Est/build_treatment.do"
do "`master'/Code/Est/export_rif.do"
do "`master'/Code/Est/confirmedrun.do"
do "`master'/Code/Est/graph_rif_ejercicio.do"


use "`master'/Data/Source/ocupados_mw.dta", clear

* Choose quantiles
glo qtiles "5 10 15 20 25 30 35 40 45 50 55 60 65 70 75 80 85 90"
 *glo qtiles "90"
* Number of bootstraps
glo boot=200
loc rerun "rerun"


* RIF 4 - Tables numbered 4 in term paper

//***********************************************//
// 					Primer Ejercicio  			 //
// 				Tratamiento en 1998q4			 //
//***********************************************//
preserve
* Choose sample
drop if time>tq(1998q4)
mysample rif4sample , hmin(30) hmax(50) sex(0) edadm(65) wage(salario) formal exind(13,29,42)
tab rif4sample
tab tipo_trabajador if rif4sample
tab year if rif4sample
tab horas_semana if rif4sample 
tab sex if rif4sample
tab afiliado_salud if rif4sample
* Build mw measures
mw_measures rif4sample , stub(rif4) time("tq(1998q3)")
* Build treatment
mytreatment, posts("tq(1998q4)") mw(bite_rif4_ind) wage(salario_mensual_real)

* RIF regressions

* Create estimation results folder if it doesn't already exist
cap mkdir "`master'/Ster"
cap mkdir "`master'/Ster/rif4_new_indcc"

* For these I'm going to assume the clusters are the city - industry pairs. This assumes that there is not correlation within city across industries, only within those pairs. Could also run with the standard clustering by city, but that would mean that I'm not really increasing the sample size by running within industry.

* The number of clusters is  enough that I don't need the full CGM framework, can run just with cluster
egen city_indcc=group(ccode cactividad_empresa)

foreach x in $qtiles {
	di " Percentile `x'"
	loc fw: word 1 of $qtiles
	loc count: word count $qtiles
	di `count'
	loc lw: word `count' of $qtiles
	if `x'==`lw' & `pr'!=0 loc reg="register"
	else loc reg ""
	if `x'==`fw' loc r="replace"
	else loc r="append"	
	* Panel B. Employment and Bartik Price Variable
	confirmedrun, `reg' controls(bartikp1_ex) name(`master'/Ster/rif4_new_indcc/reg_emp_bp_tq(1998q4)_ejercicio1) rif(rif4B) p(`x') r(`r') sample(rif4sample) fe(i.ccode#i.cactividad_empresa i.time) cluster(ccode)  `rerun'
	* Panel E. Employment, Bartik Price Variable and City Trends
	confirmedrun, `reg' controls(i.ccode#c.trend bartikp1_ex) name(`master'/Ster/rif4_new_indcc/reg_sst_emp_bp_tq(1998q4)_ejercicio1) rif(rif4E) p(`x') r(`r') sample(rif4sample) fe(i.ccode#i.cactividad_empresa i.time) cluster(ccode) `rerun'  
	* Panel N. Employment, Bartik Price Variable and national employment by industry
	confirmedrun, `reg' controls(empind bartikp1_ex) name(`master'/Ster/rif4_new_indcc/reg_ie_emp_bp_tq(1998q4)_ejercicio1) rif(rif4N) p(`x') r(`r') sample(rif4sample) fe(i.ccode#i.cactividad_empresa i.time) cluster(ccode) `rerun'
	* Panel O. Employment, Bartik Price Variable and national employment by industry, city trends
	confirmedrun, `reg' controls(empind bartikp1_ex i.ccode#c.trend) name(`master'/Ster/rif4_new_indcc/reg_iect_emp_bp_tq(1998q4)_ejercicio1) rif(rif4O) p(`x') r(`r') sample(rif4sample) fe(i.ccode#i.cactividad_empresa i.time) cluster(ccode) `rerun'
}

* Graph results and export 

clear

cap program drop mex
program define mex
	cap mkdir "`2'/Doc/ster_figures/rif4_new_indcc"
	cap mkdir "`2'/Doc/ster_figures/rif4_new_indcc/Ejercicio1"
	graph export "`2'/Doc/ster_figures/rif4_new_indcc/Ejercicio1/`1'.pdf", replace
	* ! epstopdf "`2'/Doc/ster_figures/rif4_new_indcc/Ejercicio1/`1'.eps"
	* erase "`2'/Doc/ster_figures/rif4_new_indcc/Ejercicio1/`1'.eps"
	* project, creates("`master'/Doc/descriptive_figures/density/`1'.pdf")
end	

foreach spec in reg_emp_bp reg_sst_emp_bp reg_ie_emp_bp reg_iect_emp_bp{
	graph drop _all
	graphrif, master("`master'") ster(Ster) folder(rif4_new_indcc) qtiles($qtiles) spec(`spec') time(tq(1998q4)) ej(1) rareaopts(bcolor(gs13))twowayopts(title("Formal sector - Bite - By city industry" "`spec' - 1998q4" ) xtitle(Percentile) ytitle(Coef) ytitle(Coef)  )
	mex `spec' `master'
}

restore

//***********************************************//
// 					Segundo Ejercicio 			 //
// 				Tratamiento en 1998q2/q3		 //
//***********************************************//
	cap program drop mex
	program define mex
		cap mkdir "`2'/Doc/ster_figures/rif4_new_indcc"
		cap mkdir "`2'/Doc/ster_figures/rif4_new_indcc/Ejercicio2"
		graph export "`2'/Doc/ster_figures/rif4_new_indcc/Ejercicio2/`1'_`3'.pdf", replace
		* ! epstopdf "`2'/Doc/ster_figures/rif4_new_indcc/Ejercicio2/`1'.eps"
		* erase "`2'/Doc/ster_figures/rif4_new_indcc/Ejercicio2/`1'.eps"
		* project, creates("`master'/Doc/descriptive_figures/density/`1'.pdf")
	end	

foreach tt in 1998q2 1998q3 {
	preserve
	* Choose sample
	drop if time>tq(1998q4)
	mysample rif4sample , hmin(30) hmax(50) sex(0) edadm(65) wage(salario) formal exind(13,29,42)
	tab rif4sample
	tab tipo_trabajador if rif4sample
	tab year if rif4sample
	tab horas_semana if rif4sample 
	tab sex if rif4sample
	tab afiliado_salud if rif4sample
	* Build mw measures
	loc date = tq(`tt')
	loc date = `date' - 1
	mw_measures rif4sample , stub(rif4) time("`date'")
	* Build treatment
	mytreatment, posts("tq(`tt')") mw(bite_rif4_ind) wage(salario_mensual_real)

	* RIF regressions

	* Create estimation results folder if it doesn't already exist
	cap mkdir "`master'/Ster"
	cap mkdir "`master'/Ster/rif4_new_indcc"

	* For these I'm going to assume the clusters are the city - industry pairs. This assumes that there is not correlation within city across industries, only within those pairs. Could also run with the standard clustering by city, but that would mean that I'm not really increasing the sample size by running within industry.

	* The number of clusters is  enough that I don't need the full CGM framework, can run just with cluster
	egen city_indcc=group(ccode cactividad_empresa)

	foreach x in $qtiles {
		di " Percentile `x'"
		loc fw: word 1 of $qtiles
		loc count: word count $qtiles
		di `count'
		loc lw: word `count' of $qtiles
		if `x'==`lw' & `pr'!=0 loc reg="register"
		else loc reg ""
		if `x'==`fw' loc r="replace"
		else loc r="append"	
		* Panel B. Employment and Bartik Price Variable
		confirmedrun, `reg' controls(bartikp1_ex) name(`master'/Ster/rif4_new_indcc/reg_emp_bp_tq(`tt')_ejercicio2) rif(rif4B) p(`x') r(`r') sample(rif4sample) fe(i.ccode#i.cactividad_empresa i.time) cluster(ccode)   `rerun'
		* Panel E. Employment, Bartik Price Variable and City Trends
		confirmedrun, `reg' controls(i.ccode#c.trend bartikp1_ex) name(`master'/Ster/rif4_new_indcc/reg_sst_emp_bp_tq(`tt')_ejercicio2) rif(rif4E) p(`x') r(`r') sample(rif4sample) fe(i.ccode#i.cactividad_empresa i.time) cluster(ccode)  `rerun'  
		* Panel N. Employment, Bartik Price Variable and national employment by industry
		confirmedrun, `reg' controls(empind bartikp1_ex) name(`master'/Ster/rif4_new_indcc/reg_ie_emp_bp_tq(`tt')_ejercicio2) rif(rif4N) p(`x') r(`r') sample(rif4sample) fe(i.ccode#i.cactividad_empresa i.time) cluster(ccode) `rerun'
		* Panel O. Employment, Bartik Price Variable and national employment by industry, city trends
		confirmedrun, `reg' controls(empind bartikp1_ex i.ccode#c.trend) name(`master'/Ster/rif4_new_indcc/reg_iect_emp_bp_tq(`tt')_ejercicio2) rif(rif4O) p(`x') r(`r') sample(rif4sample) fe(i.ccode#i.cactividad_empresa i.time) cluster(ccode) `rerun'
	}

	* Graph results and export 

	clear


	foreach spec in reg_emp_bp reg_sst_emp_bp reg_ie_emp_bp reg_iect_emp_bp{
		graph drop _all
		graphrif, master("`master'") ster(Ster) folder(rif4_new_indcc) qtiles($qtiles) spec(`spec') time(tq(`tt'))  ej(2) rareaopts(bcolor(gs13))twowayopts(title("Formal sector - Bite - By city industry" "`spec' - `tt'" ) xtitle(Percentile) ytitle(Coef) ytitle(Coef)  )
		mex `spec' `master' `tt'
	}

	restore
}

//***********************************************//
// 					Tercer Ejercicio 			 //
//		 	Tratamiento en 1997q1 y 1998q1		 //
//***********************************************//
	cap program drop mex
	program define mex
		cap mkdir "`2'/Doc/ster_figures/rif4_new_indcc"
		cap mkdir "`2'/Doc/ster_figures/rif4_new_indcc/Ejercicio3"
		graph export "`2'/Doc/ster_figures/rif4_new_indcc/Ejercicio3/`1'_`3'.pdf", replace
		* ! epstopdf "`2'/Doc/ster_figures/rif4_new_indcc/Ejercicio3/`1'.eps"
		* erase "`2'/Doc/ster_figures/rif4_new_indcc/Ejercicio2/`1'.eps"
		* project, creates("`master'/Doc/descriptive_figures/density/`1'.pdf")
	end	


foreach tt in 1997q1 1998q1 {
	preserve
	* Choose sample
	drop if time>tq(1998q4)
	mysample rif4sample , hmin(30) hmax(50) sex(0) edadm(65) wage(salario) formal exind(13,29,42)
	tab rif4sample
	tab tipo_trabajador if rif4sample
	tab year if rif4sample
	tab horas_semana if rif4sample 
	tab sex if rif4sample
	tab afiliado_salud if rif4sample
	* Build mw measures
	loc date = tq(`tt')
	loc date = `date' - 1
	mw_measures rif4sample , stub(rif4) time("`date'")
	* Build treatment
	mytreatment, posts("tq(`tt')") mw(bite_rif4_ind) wage(salario_mensual_real)

	* RIF regressions

	* Create estimation results folder if it doesn't already exist
	cap mkdir "`master'/Ster"
	cap mkdir "`master'/Ster/rif4_new_indcc"

	* For these I'm going to assume the clusters are the city - industry pairs. This assumes that there is not correlation within city across industries, only within those pairs. Could also run with the standard clustering by city, but that would mean that I'm not really increasing the sample size by running within industry.

	* The number of clusters is  enough that I don't need the full CGM framework, can run just with cluster
	egen city_indcc=group(ccode cactividad_empresa)

	foreach x in $qtiles {
		di " Percentile `x'"
		loc fw: word 1 of $qtiles
		loc count: word count $qtiles
		di `count'
		loc lw: word `count' of $qtiles
		if `x'==`lw' & `pr'!=0 loc reg="register"
		else loc reg ""
		if `x'==`fw' loc r="replace"
		else loc r="append"	
		* Panel B. Employment and Bartik Price Variable
		confirmedrun, `reg' controls(bartikp1_ex) name(`master'/Ster/rif4_new_indcc/reg_emp_bp_tq(`tt')_ejercicio3) rif(rif4B) p(`x') r(`r') sample(rif4sample) fe(i.ccode#i.cactividad_empresa i.time) cluster(ccode)  `rerun'
		* Panel E. Employment, Bartik Price Variable and City Trends
		confirmedrun, `reg' controls(i.ccode#c.trend bartikp1_ex) name(`master'/Ster/rif4_new_indcc/reg_sst_emp_bp_tq(`tt')_ejercicio3) rif(rif4E) p(`x') r(`r') sample(rif4sample) fe(i.ccode#i.cactividad_empresa i.time) cluster(ccode)    `rerun'
		* Panel N. Employment, Bartik Price Variable and national employment by industry
		confirmedrun, `reg' controls(empind bartikp1_ex) name(`master'/Ster/rif4_new_indcc/reg_ie_emp_bp_tq(`tt')_ejercicio3) rif(rif4N) p(`x') r(`r') sample(rif4sample) fe(i.ccode#i.cactividad_empresa i.time) cluster(ccode) `rerun'
		* Panel O. Employment, Bartik Price Variable and national employment by industry, city trends
		confirmedrun, `reg' controls(empind bartikp1_ex i.ccode#c.trend) name(`master'/Ster/rif4_new_indcc/reg_iect_emp_bp_tq(`tt')_ejercicio3) rif(rif4O) p(`x') r(`r') sample(rif4sample) fe(i.ccode#i.cactividad_empresa i.time) cluster(ccode) `rerun'
	}

	* Graph results and export 

	clear

	foreach spec in reg_emp_bp reg_sst_emp_bp reg_ie_emp_bp reg_iect_emp_bp{
		graph drop _all
		graphrif, master("`master'") ster(Ster) folder(rif4_new_indcc) qtiles($qtiles) spec(`spec') time(tq(`tt'))  ej(3) rareaopts(bcolor(gs13))twowayopts(title("Formal sector - Bite - By city industry" "`spec' - `tt'" ) xtitle(Percentile) ytitle(Coef) ytitle(Coef)  )
		mex `spec' `master' `tt'
	}

	restore
}

//***********************************************//
// 					Cuarto Ejercicio 			 //
//	 	Todo el sample sin 1eros trimestres		 //
//***********************************************//
	cap program drop mex
	program define mex
		cap mkdir "`2'/Doc/ster_figures/rif4_new_indcc"
		cap mkdir "`2'/Doc/ster_figures/rif4_new_indcc/Ejercicio4"
		graph export "`2'/Doc/ster_figures/rif4_new_indcc/Ejercicio4/`1'_`3'.pdf", replace
		* ! epstopdf "`2'/Doc/ster_figures/rif4_new_indcc/Ejercicio4/`1'.eps"
		* erase "`2'/Doc/ster_figures/rif4_new_indcc/Ejercicio2/`1'.eps"
		* project, creates("`master'/Doc/descriptive_figures/density/`1'.pdf")
	end	

foreach tt in 1999q1 {
	preserve
	* Choose sample
	drop if time>tq(2001q1)
	mysample rif4sample , hmin(30) hmax(50) sex(0) edadm(65) wage(salario) formal exind(13,29,42)
	tab rif4sample
	tab tipo_trabajador if rif4sample
	tab year if rif4sample
	tab horas_semana if rif4sample 
	tab sex if rif4sample
	tab afiliado_salud if rif4sample
	* Build mw measures
	loc date = tq(`tt')
	loc date = `date' - 1
	mw_measures rif4sample , stub(rif4) time("`date'")
	* Build treatment
	loc date = tq(`tt')
	loc date = `date' + 1
	mytreatment, posts("`date'") mw(bite_rif4_ind) wage(salario_mensual_real)
	drop if	time == tq(1996q1) | ///
			time == tq(1997q1) | ///
			time == tq(1998q1) | ///
			time == tq(1999q1) | ///
			time == tq(2000q1) | ///
			time == tq(2001q1) | ///
			time == tq(2002q1) 

	* RIF regressions

	* Create estimation results folder if it doesn't already exist
	cap mkdir "`master'/Ster"
	cap mkdir "`master'/Ster/rif4_new_indcc"

	* For these I'm going to assume the clusters are the city - industry pairs. This assumes that there is not correlation within city across industries, only within those pairs. Could also run with the standard clustering by city, but that would mean that I'm not really increasing the sample size by running within industry.

	* The number of clusters is  enough that I don't need the full CGM framework, can run just with cluster
	egen city_indcc=group(ccode cactividad_empresa)

	foreach x in $qtiles {
		di " Percentile `x'"
		loc fw: word 1 of $qtiles
		loc count: word count $qtiles
		di `count'
		loc lw: word `count' of $qtiles
		if `x'==`lw' & `pr'!=0 loc reg="register"
		else loc reg ""
		if `x'==`fw' loc r="replace"
		else loc r="append"	
		* Panel B. Employment and Bartik Price Variable
		confirmedrun, `reg' controls(bartikp1_ex) name(`master'/Ster/rif4_new_indcc/reg_emp_bp_tq(`tt')_ejercicio4) rif(rif4B) p(`x') r(`r') sample(rif4sample) fe(i.ccode#i.cactividad_empresa i.time) cluster(ccode)  `rerun'
		* Panel E. Employment, Bartik Price Variable and City Trends
		confirmedrun, `reg' controls(i.ccode#c.trend bartikp1_ex) name(`master'/Ster/rif4_new_indcc/reg_sst_emp_bp_tq(`tt')_ejercicio4) rif(rif4E) p(`x') r(`r') sample(rif4sample) fe(i.ccode#i.cactividad_empresa i.time) cluster(ccode)   `rerun' 
		* Panel N. Employment, Bartik Price Variable and national employment by industry
		confirmedrun, `reg' controls(empind bartikp1_ex) name(`master'/Ster/rif4_new_indcc/reg_ie_emp_bp_tq(`tt')_ejercicio4) rif(rif4N) p(`x') r(`r') sample(rif4sample) fe(i.ccode#i.cactividad_empresa i.time) cluster(ccode) `rerun'
		* Panel O. Employment, Bartik Price Variable and national employment by industry, city trends
		confirmedrun, `reg' controls(empind bartikp1_ex i.ccode#c.trend) name(`master'/Ster/rif4_new_indcc/reg_iect_emp_bp_tq(`tt')_ejercicio4) rif(rif4O) p(`x') r(`r') sample(rif4sample) fe(i.ccode#i.cactividad_empresa i.time) cluster(ccode) `rerun'
	}

	* Graph results and export 

	clear


	foreach spec in reg_emp_bp reg_sst_emp_bp reg_ie_emp_bp reg_iect_emp_bp{
		graph drop _all
		graphrif, master("`master'") ster(Ster) folder(rif4_new_indcc) qtiles($qtiles) spec(`spec') time(tq(`tt'))  ej(4) rareaopts(bcolor(gs13))twowayopts(title("Formal sector - Bite - By city industry" "`spec' - `tt'" ) xtitle(Percentile) ytitle(Coef) ytitle(Coef)  )
		mex `spec' `master' `tt'
	}

	restore
}
loc rerun "rerun"
//***********************************************//
// 					Quinto Ejercicio 			 //
//	 			Creciendo la muestra			 //
//***********************************************//
	cap program drop mex
	program define mex
		cap mkdir "`2'/Doc/ster_figures/rif4_new_indcc"
		cap mkdir "`2'/Doc/ster_figures/rif4_new_indcc/Ejercicio5"
		graph export "`2'/Doc/ster_figures/rif4_new_indcc/Ejercicio5/`1'_`3'.pdf", replace
		* ! epstopdf "`2'/Doc/ster_figures/rif4_new_indcc/Ejercicio5/`1'.eps"
		* erase "`2'/Doc/ster_figures/rif4_new_indcc/Ejercicio2/`1'.eps"
		* project, creates("`master'/Doc/descriptive_figures/density/`1'.pdf")
	end	

foreach tt in 1999q1 1999q2 1999q3 1999q4 {
	preserve
	* Choose sample
	drop if time>tq(`tt')
	mysample rif4sample , hmin(30) hmax(50) sex(0) edadm(65) wage(salario) formal exind(13,29,42)
	tab rif4sample
	tab tipo_trabajador if rif4sample
	tab year if rif4sample
	tab horas_semana if rif4sample 
	tab sex if rif4sample
	tab afiliado_salud if rif4sample
	* Build mw measures
	loc date = tq(`tt')
	loc date = `date' - 1
	mw_measures rif4sample , stub(rif4) time("tq(1998q4)")
	* Build treatment
	mytreatment, posts("tq(1999q1)") mw(bite_rif4_ind) wage(salario_mensual_real)

	* RIF regressions

	* Create estimation results folder if it doesn't already exist
	cap mkdir "`master'/Ster"
	cap mkdir "`master'/Ster/rif4_new_indcc"

	* For these I'm going to assume the clusters are the city - industry pairs. This assumes that there is not correlation within city across industries, only within those pairs. Could also run with the standard clustering by city, but that would mean that I'm not really increasing the sample size by running within industry.

	* The number of clusters is  enough that I don't need the full CGM framework, can run just with cluster
	egen city_indcc=group(ccode cactividad_empresa)

	foreach x in $qtiles {
		di " Percentile `x'"
		loc fw: word 1 of $qtiles
		loc count: word count $qtiles
		di `count'
		loc lw: word `count' of $qtiles
		if `x'==`lw' & `pr'!=0 loc reg="register"
		else loc reg ""
		if `x'==`fw' loc r="replace"
		else loc r="append"	
		* Panel B. Employment and Bartik Price Variable
		confirmedrun, `reg' controls(bartikp1_ex) name(`master'/Ster/rif4_new_indcc/reg_emp_bp_tq(`tt')_ejercicio5) rif(rif4B) p(`x') r(`r') sample(rif4sample) fe(i.ccode#i.cactividad_empresa i.time) cluster(ccode) `rerun'  
		* Panel E. Employment, Bartik Price Variable and City Trends
		confirmedrun, `reg' controls(i.ccode#c.trend bartikp1_ex) name(`master'/Ster/rif4_new_indcc/reg_sst_emp_bp_tq(`tt')_ejercicio5) rif(rif4E) p(`x') r(`r') sample(rif4sample) fe(i.ccode#i.cactividad_empresa i.time) cluster(ccode) `rerun'   
		* Panel N. Employment, Bartik Price Variable and national employment by industry
		confirmedrun, `reg' controls(empind bartikp1_ex) name(`master'/Ster/rif4_new_indcc/reg_ie_emp_bp_tq(`tt')_ejercicio5) rif(rif4N) p(`x') r(`r') sample(rif4sample) fe(i.ccode#i.cactividad_empresa i.time) cluster(ccode) `rerun'
		* Panel O. Employment, Bartik Price Variable and national employment by industry, city trends
		confirmedrun, `reg' controls(empind bartikp1_ex i.ccode#c.trend) name(`master'/Ster/rif4_new_indcc/reg_iect_emp_bp_tq(`tt')_ejercicio5) rif(rif4O) p(`x') r(`r') sample(rif4sample) fe(i.ccode#i.cactividad_empresa i.time) cluster(ccode) `rerun'
	}

	* Graph results and export 

	clear


	foreach spec in reg_emp_bp reg_sst_emp_bp reg_ie_emp_bp reg_iect_emp_bp{
		graph drop _all
		graphrif, master("`master'") ster(Ster) folder(rif4_new_indcc) qtiles($qtiles) spec(`spec') time(tq(`tt'))  ej(5) rareaopts(bcolor(gs13))twowayopts(title("Formal sector - Bite - By city industry" "`spec' - `tt'" ) yscale(range(-0.015 0.015)) xtitle(Percentile) ytitle(Coef) ytitle(Coef)  )
		mex `spec' `master' `tt'
	}

	restore
}

//***********************************************//
// 					Sexto Ejercicio 			 //
// 		Bite Fijo, Tratamientos Diefetentes		 //
//***********************************************//
	cap program drop mex
	program define mex
		cap mkdir "`2'/Doc/ster_figures/rif4_new_indcc"
		cap mkdir "`2'/Doc/ster_figures/rif4_new_indcc/Ejercicio6"
		graph export "`2'/Doc/ster_figures/rif4_new_indcc/Ejercicio6/`1'_`3'.pdf", replace
		* ! epstopdf "`2'/Doc/ster_figures/rif4_new_indcc/Ejercicio6/`1'.eps"
		* erase "`2'/Doc/ster_figures/rif4_new_indcc/Ejercicio6/`1'.eps"
		* project, creates("`master'/Doc/descriptive_figures/density/`1'.pdf")
	end	

foreach tt in 1998q1 1998q2 1998q3 1998q4 {
	preserve
	* Choose sample
	drop if time>tq(1998q4)
	mysample rif4sample , hmin(30) hmax(50) sex(0) edadm(65) wage(salario) formal exind(13,29,42)
	tab rif4sample
	tab tipo_trabajador if rif4sample
	tab year if rif4sample
	tab horas_semana if rif4sample 
	tab sex if rif4sample
	tab afiliado_salud if rif4sample
	* Build mw measures
	mw_measures rif4sample , stub(rif4) time("tq(1998q4)")
	* Build treatment
	mytreatment, posts("tq(`tt')") mw(bite_rif4_ind) wage(salario_mensual_real)

	* RIF regressions

	* Create estimation results folder if it doesn't already exist
	cap mkdir "`master'/Ster"
	cap mkdir "`master'/Ster/rif4_new_indcc"

	* For these I'm going to assume the clusters are the city - industry pairs. This assumes that there is not correlation within city across industries, only within those pairs. Could also run with the standard clustering by city, but that would mean that I'm not really increasing the sample size by running within industry.

	* The number of clusters is  enough that I don't need the full CGM framework, can run just with cluster
	egen city_indcc=group(ccode cactividad_empresa)

	foreach x in $qtiles {
		di " Percentile `x'"
		loc fw: word 1 of $qtiles
		loc count: word count $qtiles
		di `count'
		loc lw: word `count' of $qtiles
		if `x'==`lw' & `pr'!=0 loc reg="register"
		else loc reg ""
		if `x'==`fw' loc r="replace"
		else loc r="append"	
		* Panel B. Employment and Bartik Price Variable
		confirmedrun, `reg' controls(bartikp1_ex) name(`master'/Ster/rif4_new_indcc/reg_emp_bp_tq(`tt')_ejercicio6) rif(rif4B) p(`x') r(`r') sample(rif4sample) fe(i.ccode#i.cactividad_empresa i.time) cluster(ccode)   `rerun'
		* Panel E. Employment, Bartik Price Variable and City Trends
		confirmedrun, `reg' controls(i.ccode#c.trend bartikp1_ex) name(`master'/Ster/rif4_new_indcc/reg_sst_emp_bp_tq(`tt')_ejercicio6) rif(rif4E) p(`x') r(`r') sample(rif4sample) fe(i.ccode#i.cactividad_empresa i.time) cluster(ccode)  `rerun'  
		* Panel N. Employment, Bartik Price Variable and national employment by industry
		confirmedrun, `reg' controls(empind bartikp1_ex) name(`master'/Ster/rif4_new_indcc/reg_ie_emp_bp_tq(`tt')_ejercicio6) rif(rif4N) p(`x') r(`r') sample(rif4sample) fe(i.ccode#i.cactividad_empresa i.time) cluster(ccode) `rerun'
		* Panel O. Employment, Bartik Price Variable and national employment by industry, city trends
		confirmedrun, `reg' controls(empind bartikp1_ex i.ccode#c.trend) name(`master'/Ster/rif4_new_indcc/reg_iect_emp_bp_tq(`tt')_ejercicio6) rif(rif4O) p(`x') r(`r') sample(rif4sample) fe(i.ccode#i.cactividad_empresa i.time) cluster(ccode) `rerun'
	}

	* Graph results and export 

	clear


	foreach spec in reg_emp_bp reg_sst_emp_bp reg_ie_emp_bp reg_iect_emp_bp{
		graph drop _all
		graphrif, master("`master'") ster(Ster) folder(rif4_new_indcc) qtiles($qtiles) spec(`spec') time(tq(`tt'))  ej(6) rareaopts(bcolor(gs13))twowayopts(title("Formal sector - Bite - By city industry" "`spec' - `tt'" ) xtitle(Percentile) ytitle(Coef) ytitle(Coef)  )
		mex `spec' `master' `tt'
	}

	restore
}

 log using "`master'/Code/Est/FINrobustnessformal" , txt replace
 log close
